package com.bluetrum.abmate.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bluetrum.abmate.R;
import com.bluetrum.abmate.databinding.CardEqBinding;
import com.bluetrum.devicemanager.models.RemoteEqSetting;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public class RemoteEqAdapter extends RecyclerView.Adapter<RemoteEqAdapter.ViewHolder> {

    private Context mContext;

    private int mSpanCount;
    private int mParentWidth;

    private List<RemoteEqSetting> mEqSettings;
    private RemoteEqSetting mCurrentEqSetting;

    private int matchIndex;

    private OnItemClickListener mOnItemClickListener;

    private final String[] presetEqName = new String[] {
            "Default",
            "Pop",
            "Rock",
            "Jazz",
            "Classic",
            "Country",
    };

    public RemoteEqAdapter(Context context, List<RemoteEqSetting> eqSettings, int spanCount) {
        this.mContext = context;
        this.mSpanCount = spanCount;
        this.mEqSettings = eqSettings;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mParentWidth = parent.getWidth();

        CardEqBinding binding = CardEqBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 正方形
        int edge = mParentWidth / mSpanCount;
        holder.containerView.getLayoutParams().width = edge;
        holder.containerView.getLayoutParams().height = edge;

        holder.containerView.setOnClickListener(this::onClickCard);

        RemoteEqSetting eqSetting = mEqSettings.get(position);
        if (eqSetting.isCustom()) { // 自定义
            byte customIndex = eqSetting.getCustomIndex();
            String eqName = String.format(Locale.getDefault(), "Custom %d", customIndex + 1);
            holder.eqNameTextView.setText(eqName);
        } else { // 预设
            String eqName = presetEqName[eqSetting.getMode()];
            holder.eqNameTextView.setText(eqName);
        }
        holder.containerView.setTag(eqSetting);
        // 设置字体大小
        holder.eqNameTextView.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                mContext.getResources().getDimension(R.dimen.eq_setting_name_default)
        );

        // EQ是否当前设置，是的话高亮
        if (position == matchIndex) {
            holder.contentView.setBackgroundResource(R.color.eq_setting_current);
        } else {
            holder.contentView.setBackgroundResource(R.color.eq_setting_default);
        }
    }

    @Override
    public int getItemCount() {
        return mEqSettings != null
                ? mEqSettings.size()
                : 0;
    }

    public void setSpanCount(int spanCount) {
        this.mSpanCount = spanCount;
        notifyDataSetChanged();
    }

    private void checkSettings() {
        matchIndex = -1;

        if (mEqSettings != null) {
            for (int i = 0; i < mEqSettings.size(); i++) {
                byte mode = mEqSettings.get(i).getMode();
                byte[] gains = mEqSettings.get(i).getGains();

                if (mode == mCurrentEqSetting.getMode()
                        && Arrays.equals(gains, mCurrentEqSetting.getGains())) {
                    matchIndex = i;
                    break;
                }
            }
        }
    }

    public void setEqSettings(List<RemoteEqSetting> eqSettings) {
        this.mEqSettings = eqSettings;
        checkSettings();
        notifyDataSetChanged();
    }

    // TODO: 考虑复位
    public void setCurrentEqSetting(RemoteEqSetting eqSetting) {
        this.mCurrentEqSetting = eqSetting;

        if (mEqSettings != null) {
            RemoteEqSetting matchEqSetting = mEqSettings.stream().filter( setting ->
                    setting.getMode() == eqSetting.getMode()
            ).findFirst().get();
            // Workaround: Just replace custom EqSetting
            // TODO: Disable EqualizerView
            if (matchEqSetting.getMode() >= RemoteEqSetting.CUSTOM_START_INDEX) {
                int index = mEqSettings.indexOf(matchEqSetting);
                mEqSettings.set(index, eqSetting);
            }
        }

        checkSettings();
        notifyDataSetChanged();
    }

    private void onClickCard(View v) {
        if (mOnItemClickListener != null) {
            RemoteEqSetting eqSetting = (RemoteEqSetting) v.getTag();
            mOnItemClickListener.OnItemClick(eqSetting);
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        View containerView;
        View contentView;
        TextView eqNameTextView;
        ImageButton deleteButton;

        public ViewHolder(final CardEqBinding binding) {
            super(binding.getRoot());
            containerView = binding.getRoot();
            contentView = binding.contentView;
            eqNameTextView = binding.eqNameTextView;
            deleteButton = binding.deleteButton;
        }
    }

    public interface OnItemClickListener {
        void OnItemClick(RemoteEqSetting eqSetting);
    }

}
